<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */

    /**
     * This class is a tool to connect the php server with a mysql db.
     *
     * @package    kassiop.net
     * @subpackage database
     *
     * @author eKameleon
     * @author NairuS
     * @version 1.2 $Rev: 90 $ $Date: 2011-01-16 09:23:26 +0000 (Sun, 16 Jan 2011) $ $Author: nicolas.surian $
     */
    class MysqlConnector
    {

        /**
         * Creates a new MysqlConnector instance.
         *
         * @param  string $dbname
         * @param  string $host
         * @param  string $pass
         * @param  string $user
         * @return void
         */
        function __construct( $host, $user, $pass, $dbname )
        {
            $this->host = $host ;
            $this->user = $user ;
            $this->pass = $pass ;
            $this->name = $dbname ;
        }

        /**
         * The host of the db.
         *
         * @var string
         */
        public $host ;

        /**
         * The name of the db.
         *
         * @var string
         */
        public $name ;

        /**
         * The password of the db.
         *
         * @var string
         */
        public $pass ;

        /**
         * The name of the db user.
         *
         * @var string
         */
        public $user ;

        /**
         * Defines the mysql connection.
         *
         * @var ressource
         */
        private $link;

        /**
         * The last id inserted.
         *
         * @var integer
         */
        private $lastID ;

        /**
         * Close Mysql connection.
         *
         * @param  ressource  mysql link identifier connection.
         * @return void
         */
        public function close($link)
        {
            mysql_close($link);
        }

        /**
         * Returns the result of a MYSQL query.
         *
         * @param  string  $query The query to launch.
         * @return mixed   mysql resource if the query succeed, false otherwise
         */
        public function getResult ( $query )
        {
            // try to connect again if we lost the connection.
            $link = $this->open();

            $db = mysql_select_db ( $this->name, $link )
                or $this->throwError("Could not connect : " . mysql_error() . ' / db:' . $this->name) ;

            $result = mysql_query( $query, $link )
                or $this->throwError("Error executing query : " . mysql_error()) ;

            $this->lastID = mysql_insert_id($link) ;

            $this->close($link);

            return $result ;
        }

        /*
         * Return the lastId property.
         *
         * @return integer
         */
        public function getLastID()
        {
            return $this->lastID;
        }

        /**
         * Try to open a mysql connection.
         *
         * @return ressource
         */
        public function open()
        {
            $link = mysql_connect ( $this->host, $this->user, $this->pass )
                    or $this->throwError("Could not connect : " . mysql_error()) ;
            return $link;
        }

        /**
         * Trigger error according the debug mode.
         *
         * @param string $message
         */
        private function throwError($message)
        {
            if( !Configuration::isDebug() )
            {
                $message = 'An error has occurs. Please try later!';
            }
            trigger_error( $message );
        }
    }

?>